package com.huawei.java.main;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class Main {
//    客户节点名称
    List<String> customerName = new ArrayList<>();
//    边缘节点名称
    List<String> edgeName = new ArrayList<>();

//    List<Integer>为一条数据，数据为客户节点顺序
    List<List<Integer>> demand = new ArrayList<>();
//    List<Integer>为指定客户节点的每个边缘节点的qos
    List<List<Integer>> qosCustomer = new ArrayList<>();
//    List<Integer>为指定边缘节点的每个客户节点的qos
    List<List<Integer>> qosEdge = new ArrayList<>();
//    每个边缘节点的带宽上限
    List<Integer> siteBandwidth = new ArrayList<>();

//    qos上限
    Integer qos;

    static File fileConfig = new File("data/config.ini");
    static File fileDemand = new File("data/demand.csv");
    static File fileQos = new File("data/qos.csv");
    static File fileSiteBandwidth = new File("data/site_bandwidth.csv");

    File fileSolution = new File("output/solution.txt");

    public static void main(String[] args) throws IOException {
        readFile();
//        todo 跑算法



        writeFile();
    }



//    读文件从
    public static void readFile() throws IOException {
//        类似以下写法读csv
        BufferedReader br = new BufferedReader(new FileReader(fileDemand));
        String line = "";
        while ((line = br.readLine()) != null){
            String[] data = line.split(",");
        }
    }

//    发送gitee
    public static void sendPost() throws IOException {
        String test1 = "{\"message\": \"file upload\",\"content\": \"";
        String test2 = "\",\"access_token\": \"1c64669eee48479fd9644ab2be90f4fc\"}";
        String date = test1 + Base64.getEncoder().encodeToString(toByteArray(fileDemand)) + test2;
        System.out.println(sendPost( , data));
    }

//    写文件到
    public static void writeFile(){

    }

//    读文件到Byte数组
    public static byte[] toByteArray(File file) throws IOException {


        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bos.close();
        }
    }

//    发送post请求
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
}
